package com.crm.controller.reportsController;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.InvoiceStatusDAO;
import com.crm.dao.SupplyOrderDAO;
import com.crm.entity.Business;
import com.crm.entity.InvoiceStatus;
import com.crm.entity.SupplyOrder;


@Controller
@RequestMapping("/compBusiness")
public class CompanyBusiness {
	protected final Log logger = LogFactory.getLog(getClass());
	@RequestMapping( "/show.do") 
	public ModelAndView show(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		int busid=bus.getBusinessId();
		InvoiceStatus invstat=new InvoiceStatus();
		InvoiceStatusDAO invDAO=new InvoiceStatusDAO();
		SupplyOrderDAO soDAO=new SupplyOrderDAO();
		List<SupplyOrder> lso=soDAO.findAll(busid);
		List<InvoiceStatus> linvstat=new ArrayList<InvoiceStatus>();
		Iterator<SupplyOrder> iso=lso.iterator();
		if (iso!=null) {
			while (iso.hasNext() == true) {
				SupplyOrder so = (SupplyOrder) iso.next();
				int id = so.getId();
				InvoiceStatus inv= invDAO.findById(id, busid);
				if (inv!=null) {
					invstat.setInvoicenumber(so.getInvoice());
					invstat.setStatus(inv.getStatus());
					linvstat.add(invstat);
				}
			}
		}
		return model=new ModelAndView("/jsp/CompanyBusiness.jsp")
		.addObject("submenu", "reports")
		.addObject("menuName","reports")
		.addObject("invList",linvstat);
	}
}
